<?php
$this->pageTitle = 'Štruktúra firmy';
?>
<div class="listing tree">
	<h3>Štruktúra firmy</h3>
	<?php

	/**
	 * rekurzivny rendering zoznamu osob
	 *
	 * @param	array	$persons	zoznam osob
	 * @param	int		$parentId	id parenta
	 * @param	int		$level		level v ramci stromu
	 * @param	Object	$thisView - pridane kvoli pouzivaniu $this->Html->link
	 */
	function printPersonList($persons, $parentId, $level, $thisView, $userPermissions) {
		echo '<ul' . ($parentId ? ' id="parent-' . $parentId . '"' : '') . ($level > 1 ? ' style="display:none"' : '') . '>';
		foreach ($persons as $person) {
			echo '<li>';

			$personModel = new Person($person['Person']['id']);
			$children = $personModel->getChildren();

			if (isset($userPermissions['person']['view'])) {
				echo $thisView->Html->link(__($person['PersonType']['title'] . ' - ' . $person['Person']['full_name'], true), array('action' => 'view', $person['Person']['id']));
			} else {
				echo $person['PersonType']['title'] . ' - ' . $person['Person']['full_name'];
			}

			if ($children) {
				echo '<a class="show_tree" href="#" title="viac" onclick="$(\'parent-' . $person['Person']['id'] . '\').toggle(); return false;">';
				echo '<span class="hdn">zobraziť/skryť strom</span>';
				echo '</a>';
				printPersonList($children, $person['Person']['id'], $level + 1, $thisView, $userPermissions);
			} else {

			}
			echo '</li>';
		}
		echo '</ul>';
	}

	printPersonList($persons, 0, 1, $this, $userPermissions);

	?>
</div>